home *** CD-ROM | disk | FTP | other *** search
/ Archive Magazine CD 1995 / Archive Magazine CD 1995.iso / discs / prog_disc / volume_5 / issue_08 / boota / C-source / c / IBMtoARC < prev    next >
Encoding:
Text File  |  1991-12-16  |  18.8 KB  |  469 lines

  1. /* > c.IBMtoARC - (c) Paul Witheridge - Version 2.00 - 06 Dec 1991   */
  2.  
  3. /*===================================================================*/
  4. /*                                                                   */
  5. /*  IBMtoARC                                                         */
  6. /*  --------                                                         */
  7. /*                                                                   */
  8. /*  This utility converts IBM ASCII files to ARC ASCII files by:     */
  9. /*                                                                   */
  10. /*    1.  converting CR/LF to LF                                     */
  11. /*    2.  truncating data at CTRL-Z (0x1A) and deleting the CTRL-Z   */
  12. /*                                                                   */
  13. /*  It can process multiple files if a wildcarded filename is        */
  14. /*  specified or if a directory name is specified. For a wildcarded  */
  15. /*  filename it processes all files with names that match the        */
  16. /*  pattern. For a directory it processes all files within the       */
  17. /*  directory (and with any subordinate directories). Directory      */
  18. /*  names can also be wildcarded.                                    */
  19. /*                                                                   */
  20. /*-------------------------------------------------------------------*/
  21. /*                                                                   */
  22. /*  COPYRIGHT NOTICE                                                 */
  23. /*                                                                   */
  24. /*  IBMtoARC is subject to Copyright.                                */
  25. /*                                                                   */
  26. /*  Permission is granted by the author to any recipient of this     */
  27. /*  material to use and make/disseminate copies of the application   */
  28. /*  provided that no charges are made for doing so (other than to    */
  29. /*  cover any cost of media or postage) and that this notice is      */
  30. /*  included with all copies.                                        */
  31. /*                                                                   */
  32. /*===================================================================*/
  33.  
  34. #include "kernel.h"                     /* ARC specifics             */
  35. #include <ctype.h>                      /* Character handling        */
  36. #include <stdio.h>                      /* Input/output              */
  37. #include <stdlib.h>                     /* General utilities         */
  38. #include <string.h>                     /* String handling           */
  39.  
  40. #include "GetDirs.h"                    /* GetDirs header            */
  41. #include "ArgFuncs.h"                   /* Arg functions etc         */
  42. #include "Useful.h"                     /* Useful defns              */
  43.  
  44. /*--------------------------------------------------------------------*/
  45. /*  Global data declarations and definitions.                         */
  46. /*--------------------------------------------------------------------*/
  47.  
  48. static char flags = '\0' ;              /* Processing flags          */
  49.  
  50.   #define flgfnd 0x01                   /* Found file to process     */
  51.   #define flgovr 0x10                   /* Overwrite mode            */
  52.   #define flgrcs 0x20                   /* Recursive mode            */
  53.   #define flgtst 0x40                   /* Test mode                 */
  54.  
  55. static int filecount = 0 ;              /* Count of files processed. */
  56.  
  57. static const char *argptr = NULL ;      /* Pointer to file spec.     */
  58. static const char *pfxptr = "_" ;       /* Pointer to output prefix  */
  59.  
  60. /*-------------------------------------------------------------------*/
  61. /*  Declare functions local in scope to this file.                   */
  62. /*-------------------------------------------------------------------*/
  63.  
  64. static enum boolean cnvtfunc(           /* Convert function          */
  65.   const char *path,
  66.   direntry *ptr) ;
  67.  
  68. /*===================================================================*/
  69. /*                                                                   */
  70. /*  Main program                                                     */
  71. /*  ------------                                                     */
  72. /*                                                                   */
  73. /*  Analyse arguments. Then call the "GetDirentrys" function to      */
  74. /*  read all the file-names in the specified directory (plus any     */
  75. /*  sub directories). Provide "GetDirentrys" with pointer to         */
  76. /*  function to process the specified files.                         */
  77. /*                                                                   */
  78. /*===================================================================*/
  79.  
  80. int main( 
  81.   const int argc,                     /* Number of arguments         */
  82.   const char *const argv[])           /* Array of pointers to args   */
  83. {
  84.   /*-----------------------------------------------------------------*/
  85.   /*  Local definitions.                                             */
  86.   /*-----------------------------------------------------------------*/
  87.  
  88.   int returncode ;
  89.  
  90.   static const char options[] =  /* Used to match option argument    */
  91.   {
  92.     'O',                            /* Overwrite mode                */
  93.     'P',                            /* Output prefix                 */
  94.     'R',                            /* Recursive mode                */
  95.     'T',                            /* Test mode                     */
  96.     '0'                             /* Null byte terminator          */
  97.   } ;
  98.  
  99.   static const char **posval[] = /* Ptrs to ptrs to posit'nal values */ 
  100.   {
  101.     &argptr
  102.   } ;
  103.  
  104.   static const char optflags[] = /* Used to set option flags         */
  105.   {
  106.     flgovr,                         /* Overwrite mode                */
  107.     0,                              /* Output prefix                 */
  108.     flgrcs,                         /* Recursive mode                */
  109.     flgtst,                         /* Test mode                     */
  110.   } ;
  111.  
  112.   static const char **optval[] = /* Ptrs to ptrs to option values    */
  113.   {
  114.     NULL,                           /* No value allowed              */
  115.     &pfxptr,                        /* Output file prefix            */
  116.     NULL,                           /* No value allowed              */
  117.     NULL,                           /* No value allowed              */
  118.   } ;
  119.  
  120.   static const char helpdata[] = /* Help text to be displayed        */
  121.  
  122.     "IBMtoARC replaces pairs of carriage-return and linefeed "
  123.     "characters (CRLF = hex 0D0A) with a single linefeed "
  124.     "(LF = hex 0A) in the file(s) being processed. It also "
  125.     "truncates the file at a CTRL-Z (hex 1A) end-of-file "
  126.     "character if this is present.\x1f"
  127.     "\x1f"
  128.     "WARNING:\x01" "This utility converts files IN-PLACE if the "
  129.     "'-o' option is used to replace the input file with the "
  130.     "converted file.\x1f"
  131.     "\x1f"
  132.     "Syntax:\x01" "*IBMtoARC  [path.]object  [options]\x1f"
  133.     "\x1f"
  134.     "object:\x01" "Specifies one of the following:\x1f"
  135.     "\x01" "(a) a single non-wildcarded file name\x1f"
  136.     "\x01" "(b) a single non-wildcarded directory name\x1f"
  137.     "\x01" "(c) a wildcarded name.\x1f"
  138.     "\x1f"
  139.     "\x01" "In case (a) the file specified is converted.\x1f"
  140.     "\x1f"
  141.     "\x01" "In case (b) all files in the specified directory are "
  142.     "converted. If the RECURSION option is specified all files in "
  143.     "all subdirectories are also converted.\x1f"
  144.     "\x1f"
  145.     "\x01" "In case (c) all matching files are converted. If no "
  146.     "matching files are found, then the first matching directory "
  147.     "name is taken and all files therein are converted. If the "
  148.     "RECURSION option is specified, all matching files are "
  149.     "converted plus all files in all subdirectories.\x1f"
  150.     "\x1f"
  151.     "path:\x01" "Specifies the directory to be searched for the "
  152.     "object file/directory. If omitted the current directory "
  153.     "is searched.\x1f"
  154.     "\x1f"
  155.     "options:\x01" "Specifies processing options which can be one "
  156.     "or more ofthe following:\x1f"
  157.     "\x1f"
  158.     "\x01" "-o\x02" "-\x03" "OVERWRITE mode; output file will have "
  159.     "same name and will replace input file.\x1f"
  160.     "\x1f"
  161.     "\x01" "-p xxx\x02" "-\x03" "specify PREFIX which will be used "
  162.     "to create name for output file if '-o' is not specified; "
  163.     "'xxx' can be one or more characters including directory "
  164.     "specification (e.g. 'converted.'); if not specified a default "
  165.     "of '_' is used.\x1f"
  166.     "\x1f"
  167.     "\x01" "-r\x02" "-\x03" "RECURSION mode which causes all "
  168.     "files in all subdirectories to be converted.\x1f"
  169.     "\x1f"
  170.     "\x01-t" "\x02" "-\x03" "TEST mode; a list of files to be "
  171.     "converted is displayed but no output is actually written; "
  172.     "useful for checking when specifying a directory or "
  173.     "wildcarded filename.\x1f"
  174.     "\x1f"
  175.     "IBMtoARC - copyright Paul Witheridge, 1991\x1f"
  176.     "\x1f" ;
  177.  
  178.   static const unsigned char helptabs[] =  /* Help text tab settings */
  179.   {
  180.     1,10,17,19
  181.   } ;
  182.  
  183.   /*-----------------------------------------------------------------*/
  184.   /*  Executable statements                                          */
  185.   /*-----------------------------------------------------------------*/
  186.  
  187.   puts("\nIBMtoARC Version 2.00 - 06 December 1991\n") ;
  188.  
  189.   /*-----------------------------------------------------------------*/
  190.   /*  Analyse arguments for file-name and option flags.              */
  191.   /*-----------------------------------------------------------------*/
  192.  
  193.   analargs(argc,argv,1,posval,options,&flags,optflags,optval) ;
  194.  
  195.   /*-----------------------------------------------------------------*/
  196.   /*  Set paged scrolling mode                                       */
  197.   /*-----------------------------------------------------------------*/
  198.  
  199.   _kernel_oswrch(12) ;
  200.   _kernel_oswrch(14) ;
  201.  
  202.   puts(" PRESS SHIFT KEY TO ALLOW WINDOW TO SCROLL\n") ;
  203.   
  204.   /*-----------------------------------------------------------------*/
  205.   /*  If no file name operand just display help text.                */
  206.   /*-----------------------------------------------------------------*/
  207.  
  208.   if ( argptr == NULL )
  209.   {
  210.     displaytext(helpdata,helptabs) ;
  211.     _kernel_oswrch(15) ;
  212.     return 0 ;
  213.   }
  214.  
  215.   /*-----------------------------------------------------------------*/
  216.   /*  If TEST MODE issue message.                                    */
  217.   /*-----------------------------------------------------------------*/
  218.  
  219.   if ( flags & flgtst )
  220.   {
  221.     puts("TEST MODE (files will be identified, not converted).\n") ;
  222.   }
  223.  
  224.   /*-----------------------------------------------------------------*/
  225.   /*  Invoke GetDirs function to read the directory entry(s) of the  */
  226.   /*  file(s) to be processed. Pass it a pointer of a processing     */
  227.   /*  function to be called.                                         */
  228.   /*-----------------------------------------------------------------*/
  229.  
  230.   returncode = 4 ;
  231.  
  232.   if ( getdirentrys(argptr,
  233.        ( flags & flgrcs ? RECURSE_ALWAYS : RECURSE_ONCE ),cnvtfunc) )
  234.   {
  235.     if ( flags & flgfnd )
  236.     {
  237.       printf("\n%d file(s) ",filecount) ;
  238.       if ( flags & flgtst )
  239.       {
  240.         printf("would be ") ;
  241.       }
  242.       puts("converted.\n") ;
  243.       returncode = 0 ;
  244.     }
  245.     else
  246.     {
  247.       printf("No files found matching '%s'\n",argptr) ;
  248.       beep() ;
  249.     }
  250.   }
  251.   
  252.   /*-----------------------------------------------------------------*/
  253.   /*  Return to caller. All done.                                    */
  254.   /*-----------------------------------------------------------------*/
  255.  
  256.   _kernel_oswrch(15) ;
  257.   return returncode ;
  258. }
  259.  
  260. /*===================================================================*/
  261. /*                                                                   */
  262. /*  cnvtfunc  -  perform actual file conversion                      */
  263. /*  --------                                                         */
  264. /*                                                                   */
  265. /*  This function is called by the "getdirentrys" function for each  */
  266. /*  file-name that it encounters.                                    */
  267. /*                                                                   */
  268. /*===================================================================*/
  269.  
  270. static enum boolean cnvtfunc(
  271.   const char *path,                   /* Pointer to path name.       */
  272.   direntry *ptr)                      /* Pointer to direntry info.   */
  273.  
  274. {
  275.   char *infile ;                      /* Ptr to path + leafname      */
  276.   char *oufile ;                      /* Ptr tp path + leafname      */
  277.   int result;                         /* Result from OS-File SWI     */
  278.   char *workarea ;                    /* Ptr to start of work area   */
  279.   char *workend ;                     /* Ptr to end of work area     */
  280.   char *iptr ;                        /* Working ptr to next char    */
  281.   char *optr ;                        /* Ptr to next output char     */
  282.  
  283.   _kernel_osfile_block osfileblock ;  /* OS File parameter block     */
  284.  
  285.   /*------------------------------------------------------------------*/
  286.   /*  Translate table - IBM ASCII to ARC ASCII                        */
  287.   /*------------------------------------------------------------------*/
  288.   
  289.   static unsigned char trantabl[256] = /* No-op as it stands          */
  290.   {
  291.       0,   1,   2,   3,   4,   5,   6,   7,
  292.       8,   9,  10,  11,  12,  13,  14,  15,
  293.      16,  17,  18,  19,  20,  21,  22,  23,
  294.      24,  25,  26,  27,  28,  29,  30,  31,
  295.      32,  33,  34,  35,  36,  37,  38,  39,
  296.      40,  41,  42,  43,  44,  45,  46,  47,
  297.      48,  49,  50,  51,  52,  53,  54,  55,
  298.      56,  57,  58,  59,  60,  61,  62,  63,
  299.      64,  65,  66,  67,  68,  69,  70,  71,
  300.      72,  73,  74,  75,  76,  77,  78,  79,
  301.      80,  81,  82,  83,  84,  85,  86,  87,
  302.      88,  89,  90,  91,  92,  93,  94,  95,
  303.      96,  97,  98,  99, 100, 101, 102, 103,
  304.     104, 105, 106, 107, 108, 109, 110, 111,
  305.     112, 113, 114, 115, 116, 117, 118, 119,
  306.     120, 121, 122, 123, 124, 125, 126, 127,
  307.     128, 129, 130, 131, 132, 133, 134, 135,
  308.     136, 137, 138, 139, 140, 141, 142, 143,
  309.     144, 145, 146, 147, 148, 149, 150, 151,
  310.     152, 153, 154, 155, 156, 157, 158, 159,
  311.     160, 161, 162, 163, 164, 165, 166, 167,
  312.     168, 169, 170, 171, 172, 173, 174, 175,
  313.     176, 177, 178, 179, 180, 181, 182, 183,
  314.     184, 185, 186, 187, 188, 189, 190, 191,
  315.     192, 193, 194, 195, 196, 197, 198, 199,
  316.     200, 201, 202, 203, 204, 205, 206, 207,
  317.     208, 209, 210, 211, 212, 213, 214, 215,
  318.     216, 217, 218, 219, 220, 221, 222, 223,
  319.     224, 225, 226, 227, 228, 229, 230, 231,
  320.     232, 233, 234, 235, 236, 237, 238, 239,
  321.     240, 241, 242, 243, 244, 245, 246, 247,
  322.     248, 249, 250, 251, 252, 253, 254, 255 
  323.   } ;
  324.  
  325.   /*-----------------------------------------------------------------*/
  326.   /*  Executable statements                                          */
  327.   /*                                                                 */
  328.   /*  If test mode display heading on first time through             */
  329.   /*-----------------------------------------------------------------*/
  330.  
  331.   if ( (flags & flgtst) && !(flags & flgfnd) )
  332.   {
  333.     puts("The following files would be converted:\n") ;
  334.   }
  335.  
  336.   flags |= flgfnd ;
  337.  
  338.   /*-----------------------------------------------------------------*/
  339.   /*  Create input and output filespecs                              */
  340.   /*-----------------------------------------------------------------*/
  341.   
  342.   if ( ( infile = malloc(2 * (strlen(path) + strlen(ptr->name) + 1)
  343.                          + strlen(pfxptr) ) ) == NULL )
  344.   {
  345.     puts("Out of memory") ;
  346.     return FALSE ;
  347.   } 
  348.   
  349.   oufile = infile + sprintf(infile,"%s%s",path,ptr->name) + 1 ;
  350.   sprintf(oufile,"%s%s%s",path,flags & flgovr ? "" : pfxptr,ptr->name) ;
  351.   
  352.   /*-----------------------------------------------------------------*/
  353.   /*  Check input file has non-zero length                           */
  354.   /*-----------------------------------------------------------------*/
  355.  
  356.   if ( ptr->length == 0 )
  357.   {
  358.     printf("'%s' is empty\n",infile) ;
  359.   }
  360.  
  361.   /*-----------------------------------------------------------------*/
  362.   /*  Allocate memory for file and load file                         */
  363.   /*-----------------------------------------------------------------*/
  364.  
  365.   if ( (workarea = malloc((ptr->length)+1) ) == NULL )
  366.   {
  367.     printf("'%s' too large to load",infile) ;
  368.     goto error4 ;
  369.   }
  370.  
  371.   workend = workarea + ptr->length ;
  372.  
  373.   osfileblock.load  = (int)workarea ;
  374.   osfileblock.exec  = 0 ;
  375.   result = _kernel_osfile(16,infile,&osfileblock) ;
  376.  
  377.   if (result == _kernel_ERROR)
  378.   {
  379.     printf("'%s' load failed - %s\n",infile,
  380.           _kernel_last_oserror()->errmess) ;
  381.     goto error3 ;
  382.   }
  383.  
  384.   /*-----------------------------------------------------------------*/
  385.   /*  Convert CRLF combinations to Archimedes newline characters     */
  386.   /*  (linefeed only) and truncate file at CTRL-Z.                   */
  387.   /*-----------------------------------------------------------------*/
  388.  
  389.   *workend = 0 ;
  390.  
  391.   for ( iptr = optr = workarea ; iptr < workend ; iptr++, optr++ )
  392.   {
  393.     if ( *iptr == 0x1A )
  394.     {
  395.       break ;
  396.     }
  397.     if ( *iptr == 0x0d && *(iptr+1) == 0x0A )
  398.     {
  399.       iptr++;
  400.     }
  401.     *optr = trantabl[*iptr] ;
  402.   }
  403.  
  404.   workend = optr ;
  405.   
  406.   /*-----------------------------------------------------------------*/
  407.   /*  If not test mode, save modified file                           */
  408.   /*-----------------------------------------------------------------*/
  409.  
  410.   if ( !(flags & flgtst) )
  411.   {
  412.     osfileblock.load  = ptr->load ;
  413.     osfileblock.exec  = ptr->exec ;
  414.     osfileblock.start = (int)workarea ;
  415.     osfileblock.end   = (int)optr ;
  416.  
  417.     result = _kernel_osfile(0,oufile,&osfileblock) ;
  418.  
  419.     if (result == _kernel_ERROR)
  420.     {
  421.       printf("'%s' save failed - %s\n",oufile,
  422.              _kernel_last_oserror()->errmess);
  423.       goto error3 ;
  424.     }
  425.   }
  426.  
  427.   /*-----------------------------------------------------------------*/
  428.   /*  Valid conversion - bump file count                             */
  429.   /*-----------------------------------------------------------------*/
  430.   
  431.   filecount++ ;
  432.  
  433.   /*-----------------------------------------------------------------*/
  434.   /*  Display file name (plus "converted" if not test mode)          */
  435.   /*-----------------------------------------------------------------*/
  436.  
  437.   printf("'%s'",infile) ;
  438.   if ( !(flags & flgtst) )
  439.   {
  440.     printf(" converted") ;
  441.     if ( !(flags & flgovr) )
  442.     {
  443.       printf(" and saved as '%s'",oufile) ;
  444.     }
  445.   }
  446.   putchar('\n') ;
  447.  
  448.   /*-----------------------------------------------------------------*/
  449.   /*  Free work area and return with good completion code            */
  450.   /*-----------------------------------------------------------------*/
  451.  
  452.   free(workarea) ;
  453.   free(infile) ;
  454.  
  455.   return TRUE ;
  456.  
  457.   /*-----------------------------------------------------------------*/
  458.   /*  Error exits                                                    */
  459.   /*-----------------------------------------------------------------*/
  460.  
  461.   error3: free(workarea) ;
  462.   error4: free(infile) ;
  463.           beep() ;
  464.  
  465.   return FALSE ;
  466. }
  467.  
  468. /*=====================================================================*/
  469.